package net.zabuchy.activities.action;
 
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.management.StringValueExp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import net.zabuchy.activities.entity.HistoryAttribute;
import net.zabuchy.helper.ActivityHelper;
import net.zabuchy.helper.ParamNames;
import net.zabuchy.issue.bo.IssueBo;
import net.zabuchy.issue.entity.Issue;

import com.opensymphony.xwork2.Action;

 
public class EstimateAction extends ActivityAction implements 
		ServletRequestAware,ServletResponseAware {
 
    private static final Logger LOGGER = Logger.getLogger(EstimateAction.class.getName());
 
    @Autowired
    private IssueBo issueBo;
    
    private HttpServletRequest request;
    private HttpServletResponse response;
    
    private String estimation;
    private Date estimationDate;
    
    private int issueId;
    
    
    
    public int getIssueId() {
		return issueId;
	}

	public void setIssueId(int issueId) {
		this.issueId = issueId;
	}

	public String execute() {
    	LOGGER.debug("EstimateAction issueId: " + issueId);
    	Set<HistoryAttribute> attributes = new HashSet<HistoryAttribute>(0);
    	
    	LOGGER.debug("dupa");
    	Issue issue = issueBo.findById(issueId);
    	LOGGER.debug("dupa");
    	
    	if(!StringUtils.isEmpty(estimation)) {
    		LOGGER.debug("dodana estymata hours");
    		attributes.add(new HistoryAttribute("estimation", estimation, null));
    		int estimationInteger = Integer.parseInt(estimation);
        	issue.setEstimationHours(estimationInteger);
    	}
    	if(estimationDate != null) {
    		LOGGER.debug("dodana estymata time");
    		attributes.add(new HistoryAttribute("estimationDate", estimationDate.toString(), null));
    		issue.setEstimationTime(estimationDate);
    	}
    	
    	issueBo.update(issue);
    	saveActivityEvent(issueId, attributes, ParamNames.ESTIMATE);
    	
    	return Action.SUCCESS;
    }
    
	public IssueBo getIssueBo() {
		return issueBo;
	}

	public void setIssueBo(IssueBo issueBo) {
		this.issueBo = issueBo;
	}

	public String getEstimation() {
		return estimation;
	}

	public void setEstimation(String estimation) {
		this.estimation = estimation;
	}

	public Date getEstimationDate() {
		return estimationDate;
	}

	public void setEstimationDate(Date estimationDate) {
		this.estimationDate = estimationDate;
	}

	public void setServletRequest(HttpServletRequest request){
		  this.request = request;
		  }

		  public HttpServletRequest getServletRequest(){
		  return request;
		  }

		  public void setServletResponse(HttpServletResponse response){
		  this.response = response;
		  }

		  public HttpServletResponse getServletResponse(){
		  return response;
		  }

	
}




